package com.hqyj.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.common.pojo.TestPaper;
import com.hqyj.common.dao.TestPaperMapper;
import com.hqyj.common.service.ITestPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

/**
 * <p>
 * 试卷 服务实现类
 * </p>
 *
 * @author j0701-9
 * @since 2021-10-20
 */
@Service
public class TestPaperServiceImpl implements ITestPaperService {
    @Autowired
    TestPaperMapper testPaperMapper;

    @Override
    public HashMap<String, Object> paperList(TestPaper testPaper) {
        HashMap<String, Object> map = new HashMap<>();
        List<TestPaper> testPapers = testPaperMapper.selectList(null);
        map.put("testPaperList", testPapers);
        return map;
    }


    public HashMap<String, Object> queryAllExam(TestPaper testPaper) {
        HashMap<String, Object> map = new HashMap<>();
        //创建分页对象
        System.out.println(testPaper.getPage() + "--------testPaper.getPage()--------");
        System.out.println(testPaper.getRow() + "--------testPaper.getPage()--------");
        PageHelper.startPage(testPaper.getPage(), testPaper.getRow());
        //自定义查询
        List<TestPaper> list = new ArrayList<>();
        list = testPaperMapper.selectAll();
        //第三步获取分页的结果集
        PageInfo<TestPaper> pageMap = new PageInfo<TestPaper>(list);
        for (TestPaper paper : pageMap.getList()) {
            System.out.println(paper.getTestPaperId() + "===================");
        }
        if (null != pageMap) {
            //当前页的结果集
            map.put("list", pageMap.getList());
            //总条数
            map.put("total", pageMap.getTotal());
            //上一页
            map.put("prePage", pageMap.getPrePage());
            //下一页
            map.put("nextPage", pageMap.getNextPage());
            //当前页
            map.put("curPage", pageMap.getPageNum());
            //总页数
            map.put("totalPage", pageMap.getPages());
        } else {
            map.put("list", false);
        }
        return map;
    }

    @Override
    public HashMap<String, Object> addPaper(TestPaper testPaper) {
        HashMap<String, Object> map = new HashMap<>();
        if (testPaperMapper.insert(testPaper) > 0) {
            map.put("message", "保存成功！");
        } else {
            map.put("message", "保存失败！");
        }
        return map;
    }

    public HashMap<String, Object> updataTestPapper(TestPaper testPaper) {
        QueryWrapper<TestPaper> queryWrapper = new QueryWrapper<>();
        HashMap<String, Object> map = new HashMap<>();
        int flag = testPaperMapper.updateById(testPaper);
        if (flag > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> updatePaper(TestPaper testPaper) {
        HashMap<String, Object> map = new HashMap<>();
        if (testPaperMapper.updateById(testPaper) > 0) {
            map.put("message", "保存成功！");
        } else {
            map.put("message", "保存失败！");
        }
        return map;
    }

    public HashMap<String, Object> deleteTestPapper(Integer id) {
        //QueryWrapper<Integer> queryWrapper = new QueryWrapper<>();
        HashMap<String, Object> map = new HashMap<>();
        int flag = testPaperMapper.deleteById(id);
        if (flag >= 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> addExam(TestPaper testPaper) {
        HashMap<String, Object> map = new HashMap<>();
        int n = testPaperMapper.insert(testPaper);
        if (n > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> deletePaper(Integer testPaperId) {
        HashMap<String, Object> map = new HashMap<>();
        if (testPaperMapper.deleteById(testPaperId) > 0) {
            map.put("message", "保存成功！");
        } else {
            map.put("message", "保存失败！");
        }
        return map;
    }
}